package innerfunc;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.junit.jupiter.api.Test;

//4个内置函数接口
public class InnerFuncDemo {
	/**
	供应商: 无参，有返
	public interface Supplier<T> {
	    T get();
	}
	*/
	@Test
	public void supplier() {
		Supplier<Integer> sup = new Supplier<Integer>() {
			@Override
			public Integer get() {
				Random rand = new Random();
				return rand.nextInt(100);
			}
		};
		Integer num = sup.get();
		System.out.println(num);
	}
	
	/**
	 * 消费者： 有参无返
	 * public interface Consumer<T> {
	 *   void accept(T t);
	 * }
	 */
	@Test
	public void consumer() {
		Consumer<String> con = new Consumer<String>() {
			@Override
			public void accept(String t) {
				System.out.println(t);
			}
		};
		con.accept("hello kitty");
	}
	
	/**
	 * 函数： 有参有返
	 * public interface Function<T, R> {
	 *	  R apply(T t);
	 * }
	 */
	@Test
	public void function() {
		Function<Double,Double> fun = new Function<Double,Double>() {
			@Override
			public Double apply(Double t) {
				return (double) Math.round(t); //四舍五入取整
			}
		};
		Double num = fun.apply(1.55);
		System.out.println(num);
	}
	
	/**
	 * 断言：有参返回boolean
	 * public interface Predicate<T> {
	 *   boolean test(T t);
	 * }
	 */
	@Test
	public void predicate() {
		Predicate<String> pre = new Predicate<String>() {
			@Override
			public boolean test(String t) {
				return t.length() > 6;
			}
		};
		
		boolean test = pre.test("hello world");
		System.out.println(test);
	}
}
